<script lang="ts" setup name="Goods">
import useStore from '@/store'
import { storeToRefs } from 'pinia'
import { ref, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import Message from '@/components/message'
import { CartItem } from '@/types/cart'

const { goods, cart } = useStore()
const route = useRoute()
watchEffect(() => {
  const id = route.params.id as string
  if (route.fullPath !== `/goods/${id}`) return
  goods.resetInfo()
  goods.getGoodsInfo(id)
})
const { info } = storeToRefs(goods)
const currentSkuId = ref(
  route.params.id === '1369155859933827074' ? '1369155864430120962' : ''
)
const changeSku = (skuId: string) => {
  currentSkuId.value = skuId
  const sku = info.value.skus.find((item) => item.id === skuId)
  if (sku) {
    info.value.inventory = sku.inventory
    info.value.price = sku.price
    info.value.oldPrice = sku.oldPrice
  }
}

// 加入购物车
const count = ref(1)
const addCart = async () => {
  if (!currentSkuId.value) {
    return Message.warning('请选择完整信息')
  }
  const sku = info.value.skus.find((item) => item.id === currentSkuId.value)
  const attrsText = sku?.specs
    .map((item) => item.name + ':' + item.valueName)
    .join(' ')
  console.log(attrsText, count.value)
  await cart.addCart({
    // 本地添加
    id: info.value.id,
    name: info.value.name,
    picture: info.value.mainPictures[0],
    price: info.value.price,
    count: count.value,
    skuId: currentSkuId.value,
    attrsText: attrsText,
    selected: true,
    nowPrice: info.value.price,
    stock: info.value.inventory,
    isEffective: true
  } as CartItem)
  count.value = 1
  Message.success('添加成功')
}
</script>
<template>
  <div class="xtx-goods-page" v-if="info.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${info.categories[1].id}`">{{
          info.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${info.categories[0].id}`">{{
          info.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ info.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="info.mainPictures"></GoodsImage>
          <GoodsSales />
        </div>
        <div class="spec">
          <GoodsName :goods="info" />
          <!-- 规格组件 -->
          <!-- bug修复方法一：:key="info.id" -->
          <GoodsSku
            :goods="info"
            skuId="1369155864430120962"
            @change-sku="changeSku"
          ></GoodsSku>
          <!-- 数字选择框 -->
          <XtxNumbox showLabel v-model="count"></XtxNumbox>
          <XtxButton type="primary" style="margin-top: 20px" @click="addCart"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsDetail :goods="info"></GoodsDetail>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
          <GoodsHot :type="3"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
